#region Using

using System;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BlogEngine.Core;
using System.Xml;
using System.Data;
using Artem.Web.UI.Controls;

#endregion

public partial class widgets_GoogleMaps_widget : WidgetBase
{
	public override void LoadWidget()
	{
        StringDictionary settings = GetSettings();
        XmlDocument doc = new XmlDocument();
        if (settings["content"] != null)
            doc.InnerXml = settings["content"];
        XmlNodeList list = doc.SelectNodes("//marker");
        if (list.Count > 0)
        {
            using (XmlTextReader reader = new XmlTextReader(doc.OuterXml, XmlNodeType.Document, null))
            {
                System.Data.DataSet ds = new System.Data.DataSet();
                ds.ReadXml(reader);

                bool AddedMapAddress = false;

                GoogleMap googlemap = new GoogleMap();

                //Add your site key insted of mine
                //Retrive key at: http://code.google.com/apis/maps/signup.html 
                //> Kristian
                googlemap.Key = "ABQIAAAA3Bov1d19YRo_V_-9i4050BTLl16PvIZH2k2ww0Ak2dNtBpPyaxQ3_TItHrzz1YwMA16KE5vzTGrUPg";

                googlemap.Width = Unit.Parse("400px");
                googlemap.Height = Unit.Parse("200px");
                googlemap.Zoom = 4;

                foreach(DataRow r in ds.Tables[0].Rows)
                {
                    double Latitude = 0;
                    double Longitude = 0;

                    double.TryParse(r["Latitude"].ToString(), out Latitude);
                    double.TryParse(r["Longitude"].ToString(), out Longitude);

                    string Title = r["Title"].ToString();
                    string Description = r["Description"].ToString();
                    string Address = r["Address"].ToString();

                    if (Latitude > 0 && Longitude > 0)
                    {
                        GoogleMarker mark = new GoogleMarker(Latitude, Longitude);
                        mark.Address = Address;
                        mark.Title = Title;
                        mark.Text = Description;

                        googlemap.Markers.Add(mark);

                        if (!AddedMapAddress)
                        {
                            AddedMapAddress = true;
                            googlemap.Latitude = Latitude;
                            googlemap.Longitude = Longitude;
                        }
                    }
                    else
                    {
                        GoogleMarker mark = new GoogleMarker(Address);
                        mark.Title = Title;
                        mark.Text = Description;

                        googlemap.Markers.Add(mark);

                        if (!AddedMapAddress)
                        {
                            AddedMapAddress = true;
                            googlemap.Address = Address;
                        }
                    }
                }

                if (googlemap.Markers.Count > 0)
                    plcMap.Controls.Add(googlemap);
                //grd.DataSource = ds;               
                //grd.DataBind();
                ds.Dispose();
            }
        }
   }

	/// <summary>
	/// Gets the name. It must be exactly the same as the folder that contains the widget.
	/// </summary>
	/// <value></value>
	public override string Name
	{
		get { return "GoogleMap"; }
	}

	/// <summary>
	/// Gets wether or not the widget can be edited.
	/// <remarks>
	/// The only way a widget can be editable is by adding a edit.ascx file to the widget folder.
	/// </remarks>
	/// </summary>
	/// <value></value>
	public override bool IsEditable
	{
		get { return true; }
	}

}
